/*
 * This file is part of LazyCipher. LazyCipher is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version. LazyCipher is distributed in
 * the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details. You should have received a
 * copy of the GNU General Public License along with LazyCipher. If not, see
 * <http://www.gnu.org/licenses/>.
 */
package org.lazycoder.Crypto.Ciphers;

import java.util.Iterator;

import org.lazycoder.Crypto.Letter;
import org.lazycoder.Crypto.Message;
import org.lazycoder.Crypto.Keys.Key;

/**
 * @author Matthew Bayliss
 */
public class Caesar extends Cipher {
	public Caesar() {
		name = "Caesar Cipher";
		Description = "In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a shift of 3, A would be replaced by D, B would become E, and so on. The method is named after Julius Caesar, who used it in his private correspondence.";
		URL = "http://en.wikipedia.org/wiki/Caesar_cipher";
	}

	@Override
	public Message encrypt(Message message, Key key) {
		Iterator<Letter> itr = message.getIterator();
		while (itr.hasNext()) {
			Letter l = itr.next();
			l.add(key.getInteger());
		}
		return message;
	}

	@Override
	public Message decrypt(Message message, Key key) {
		Iterator<Letter> itr = message.getIterator();
		while (itr.hasNext()) {
			Letter l = itr.next();
			l.sub(key.getInteger());
		}
		return message;
	}

}
